import ElementFactory from 'diagram-js/lib/core/ElementFactory'
import Create from 'diagram-js/lib/features/create/Create'
import { Connection, Label, Root, Shape } from 'diagram-js/lib/model/Types'
import { assign } from 'min-dash'
export function createAction(
  elementFactory: ElementFactory<Connection, Label, Root, Shape>,
  create: Create,
  type: string,
  group: string,
  className: string,
  title: string,
  options?: { isExpanded?: boolean; [key: string]: any } // ✅ 明确options类型，避免any
) {
  function createListener(event: unknown) {
    // 1. 合并基础配置和options，避免重复assign
    const shapeConfig = { type, ...options };
    const shape = elementFactory.createShape(shapeConfig);

    // 2. 优化isExpanded赋值：用可选链+类型守卫，避免强制断言
    if (options?.isExpanded !== undefined) { // 仅当isExpanded有值时才赋值
      if (!shape.businessObject.di) {
        shape.businessObject.di = {};
      }
      // 明确di的类型（bpmn-js中di为DiagramElement，包含isExpanded属性）
      shape.businessObject.di.isExpanded = options.isExpanded;
    }

    create.start(event, shape);
  }

  return {
    group,
    className,
    title,
    action: {
      dragstart: createListener,
      click: createListener
    }
  };
}